package com.pinyougou.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pinyougou.mapper.TbBrandMapper;
import com.pinyougou.pojo.PageResult;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.pojo.TbBrandExample;
import com.pinyougou.sellergoods.service.BrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * Created by WF on 2019-11-07 11:21
 */
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private TbBrandMapper brandMapper;
    @Override
    public List<TbBrand> findAll() {
        return brandMapper.selectByExample(null);
    }

//    分页查询
    @Override
    public PageResult findPage(int pageNum, int pageSize) {
        //
        PageHelper.startPage(pageNum,pageSize);
        //
        List<TbBrand> tbBrands = brandMapper.selectByExample(null);
        //
        Page<TbBrand> list= (Page<TbBrand>) tbBrands;
        //
        return new PageResult(list.getTotal(),list.getResult());
    }
    //添加品牌
    @Override
    public void add(TbBrand brand) {
        brandMapper.insertSelective(brand);
    }
    //修改品牌
    @Override
    public void update(TbBrand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    @Override
    public TbBrand findOne(Long id) {
        return null;
    }

    //根据id批量删除
    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            //根据id循环遍历删除品牌
            brandMapper.deleteByPrimaryKey(id);
        }

    }
//    带条件分页查询
    @Override
    public PageResult findPage(TbBrand brand, int pageNum, int pageSize) {
        //开始进行分页
        PageHelper.startPage(pageNum,pageSize);
        //定义查询对象
        TbBrandExample example=new TbBrandExample();
        //创建查询条件对象
        TbBrandExample.Criteria criteria = example.createCriteria();
        //判断brand是否为空，为空则不带任何条件查询
        if (brand!=null){
            //判断模糊查询条件品牌名字是否为空
            if(StringUtils.isNotBlank(brand.getName())){
                criteria.andNameLike("%"+brand.getName()+"%");
            }
            //判断模糊查询条件品牌首字母是否为空
            if(StringUtils.isNotBlank(brand.getFirstChar())){
                criteria.andFirstCharEqualTo(brand.getFirstChar());
            }
        }
        //进行查询
        List<TbBrand> tbBrands = brandMapper.selectByExample(example);
        //将数据转成分页对象
        Page<TbBrand> list= (Page<TbBrand>) tbBrands;
        //返回
        return new PageResult(list.getTotal(),list.getResult());
    }

    @Override
    public List<Map> findBrandList() {
        return brandMapper.selectBrandList();
    }
}
